﻿using System;

namespace IniFileManager
{
	/// <summary>
	/// Class which stores all ini file.
	/// </summary>
	sealed public class IniData
	{
		private IniFileStructure fileStructure;
		private UniqueList<IniSection> sections;

		/// <summary>
		/// Contructor of IniData.
		/// </summary>
		public IniData()
		{
			this.sections = new UniqueList<IniSection>();
			this.fileStructure = new IniFileStructure();
		}

		/// <summary>
		/// Gets sections in data.
		/// </summary>
		public UniqueList<IniSection> Sections
		{
			get
			{
				return this.sections;
			}
		}

		/// <summary>
		/// Gets or sets structure which represents physical file organization.
		/// </summary>
		internal IniFileStructure FileStructure
		{
			get
			{
				return fileStructure;
			}
			set
			{
				fileStructure = value;
			}
		}

		/// <summary>
		/// Method which add new section to data.
		/// </summary>
		/// <param name="sectionName">Name of new section.</param>
		public void AddSection( string sectionName )
		{
			IniSection newSection = new IniSection( sectionName );

			AddSection( newSection );
		}

		/// <summary>
		/// Method which add new section to data.
		/// </summary>
		/// <param name="newSection">New section.</param>
		public void AddSection( IniSection newSection )
		{
			try
			{
				this.sections.Add( newSection );
			}
			catch( ArgumentException )
			{
				throw new FormatException( "\"" + newSection.Name + "\" section already exists." );
			}
		}

		/// <summary>
		/// Method which removes section from data.
		/// </summary>
		/// <param name="sectionName">Name of deleted section.</param>
		public void RemoveSection( string sectionName )
		{
			try
			{
				this.sections.Remove( sectionName );
			}
			catch( ArgumentException )
			{
				throw new FormatException( "\"" + sectionName + "\" section does not exists." );
			}
		}

		/// <summary>
		/// Method which removes section from data.
		/// </summary>
		/// <param name="section">Deleted section.</param>
		public void RemoveSection( IniSection section )
		{
			try
			{
				this.sections.Remove( section );
			}
			catch( ArgumentException )
			{
				throw new FormatException( "\"" + section.Name + "\" section does not exists." );
			}
		}
	}
}
